package com.itcc.config;

import com.itcc.controller.interceptor.ProjectInterceptor;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Getter
public class SpringMvcSupportConfig extends WebMvcConfigurationSupport {


    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Autowired
    private AppDownConfig appDownConfig;

    // 配置静态资源映射
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webapp/**").addResourceLocations("/webapp/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
        registry.addResourceHandler("/WEB-INF/**").addResourceLocations("/WEB-INF/");
        registry.addResourceHandler("/download/**").addResourceLocations("file:" + appDownConfig.getDownloadDir());
    }

    //  注册拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //配置拦截的请求路径
//        registry.addInterceptor(projectInterceptor).addPathPatterns("/commService", "/commService/*");
        registry.addInterceptor(projectInterceptor)
                .addPathPatterns("/commService/**")       // 拦截所有 commService 下的请求
                .addPathPatterns("/fileService/**")       // 拦截所有 fileService 下的请求
                .excludePathPatterns(
                        "/commService/login",             // 登录接口放行
                        "/commService/register",           // 注册接口放行
                        "/commService/captcha",            // 验证码接口放行
                        "/commService/exitU",             // 退出登录放行
                        "/js/**",                          // 静态资源放行
                        "/plugins/**",
                        "/webapp/**",
                        "/WEB-INF/**"
                );

    }

}
